package com.zy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zy.bean.TRole;
import com.zy.service.TRolePermissionService;
import com.zy.service.TRoleService;
import com.zy.util.Datas;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 赵阳
 * @Date 2023/11/26 18:43
 * @Version 1.5
 * @name 方法名  角色用户表的增删改查  （异步数据）1
 */
@Controller
public class TRoleController {
    @Autowired
    TRoleService tRoleService;
    @Autowired
    TRolePermissionService tRolePermissionService;
    Logger log = LoggerFactory.getLogger(TRoleController.class);

    /**
     * @Author: zy 690748807@qq.com
     * @Param:
     * @return:
     * @Date: 2024/1/15
     * @Description: 跳转到角色列表页面
     */
    @RequestMapping("/role/index")
    public String index() {

        return "role/index";
    }

    /**
     * @Author zy 690748807@qq,com
     * @Date ${DATE} ${TIME}
     * @Version 1.5
     * @name 方法名 按条件查询和查询所有 http://localhost:8080/zy_zc_main_war/role/loadData
     */
    @ResponseBody
    @RequestMapping("/role/loadData")
    public PageInfo<TRole> loadData(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                    @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                    Model model,
                                    @RequestParam(value = "condition", required = false, defaultValue = "") String condition) {
        PageHelper.startPage(pageNum, pageSize);  //线程绑定

        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("condition", condition);
        //分页查询所有
        PageInfo<TRole> page = tRoleService.listRolePage(paramMap);
        model.addAttribute("page", page);
        return page;
    }
    /**
     * 启用消息转换器：HttpMessageConverter
     *
     * 如果返回结果为对象（Entity Class,List,Map..）类型：启用这个转换器->MappingJackson2HttpMessageConverter 将对象序列化为json串，使用Jackson组件转换
     * 如果返回结果为String类型：启用这个转换器->StringHttpMessageConverter  将字符串原样输出。
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
//    @ResponseBody
//    @RequestMapping("/role/loadData")
//    public PageInfo<TRole> loadData(@RequestParam(value="pageNum",required=false,defaultValue="1") Integer pageNum,
//                                    @RequestParam(value="pageSize",required=false,defaultValue="2") Integer pageSize,
//                                    @RequestParam(value="condition",required=false,defaultValue="")  String condition,Model model) {
//
//        PageHelper.startPage(pageNum, pageSize);
//
//        Map<String,Object> paramMap = new HashMap<String,Object>();
//
//        paramMap.put("condition",condition);
//
//        PageInfo<TRole> page = tRoleService.listRolePage(paramMap) ;
//        model.addAttribute("page", page);
//        return page;//转换为json串返回
//    }
    /**
     * @Author zy 690748807@qq,com
     * @Date ${DATE} ${TIME}
     * @Version 1.5
     * @name 方法名  添加
     */
    @PreAuthorize("hasRole('PM - 项目经理')")//只有是项目经理的角色才可以添加哦
    @ResponseBody
    @RequestMapping("/role/doAdd")
    public String addRole(TRole tRole) {
        tRoleService.instRole(tRole);
        return "ok";
    }

    /**
     * @Author zy 690748807@qq,com
     * @Date ${DATE} ${TIME}
     * @Version 1.5
     * @name 方法名  修改
     */
//    @ResponseBody
//    @RequestMapping("/role/doUpdate")
//    public void updateRole(Integer id,String name){
//        TRole tRole = tRoleService.selectbyId(id);
//        tRole.setName(name);
//
//        tRoleService.updateByPrimaryKeySelective(tRole);
//
//    }
    @ResponseBody
    @RequestMapping("/role/doUpdate")
    public String doUpdate(TRole role) {
        tRoleService.updateByPrimaryKeySelective(role);
        return "ok";
    }

    @ResponseBody
    @RequestMapping("/role/getRoleById")
    public TRole getRoleById(Integer id) {
        return tRoleService.getRoleById(id);
    }

    /**
     * @Author zy 690748807@qq,com
     * @Date ${DATE} ${TIME}
     * @Version 1.5
     * @name 方法名  删除
     */
    @ResponseBody
    @RequestMapping("/role/doDelete")
    public String delRole(Integer id) {
        tRoleService.delRole(id);
        return "ok";
    }


    @RequestMapping("/role/deleteBatchBtn")
    public String delselectRole(@Param("ids") String ids) {
        String[] idList = ids.split(",");
        //把字符串数组转成list集合
        List<Integer> list = new ArrayList<Integer>();
        for (String s : idList) {
            Integer id = Integer.parseInt(s);
            list.add(id);
        }
        tRoleService.delselectRole(list);
        return "redirect:/role/index";  //添加完了回到当前一页
    }

    /**
     * @Author: zy 690748807@qq.com
     * @Param: /role/doAssignPermissionToRole
     * @return:
     * @Date: 2024/1/19
     * @Description:角色分配权限
     */
    @ResponseBody
    @PostMapping("/role/doAssignPermissionToRole")
    public String doAssignPermissionToRole(Integer roleId, Datas ds) {
        log.debug("ds={}", ds.getIds());
        log.debug("roleId={}", roleId);

        tRolePermissionService.savePermissionToRole(roleId,ds);
        return "ok";  //添加完了回到当前一页
    }


    /**
    *@Author: zy 690748807@qq.com
    * @Param:
    * @return:
    * @Date: 2024/1/20
    * @Description:角色分配权限后进行回显示
    */
    @ResponseBody
    @RequestMapping("/role/listPermissionIdByRoleId")
    public List<Integer> listPermissionIdByRoleId(Integer roleId) {
        log.debug("roleId={}", roleId);
      List<Integer> list=  tRolePermissionService.listPermissionIdByRoleId(roleId);
        return list;
    }
}
